const http = require('./utils/http.js');
const util = require('./utils/util.js');
const base64 = require('./utils/base64.js');
App({
  //登陆之后 生成 对象
  onLaunch(options) {
  
  },
 

  globalData: {
    
  },
  http: http,
  util: util,
  //判断是否登录
  check: function (cb) {
    var that = this;
    if (this.getUserToken()) {
      typeof cb == "function" && cb(this.globalData.auth);
    } else {
      //未登录跳转到登陆页面
      wx.reLaunch({
        url: '/pages/login/service'
      });
    }
  },
  getUserToken: function () {
    var userinfo = this.globalData.auth;
    if (userinfo && typeof userinfo == 'object' && userinfo.token) {
      // 还要检查token是否过期
      let date = new Date();
      var now = date.getTime() / 1000;
      if (now >= (userinfo.expiretime - 20)) {
      
        return false;
      }
  
     
      return userinfo.token;
    }

    return false;
  },
  getV3Sass() {
    var userinfo = this.globalData.auth;
    let key = this.globalData.key;
    let token = ''
    console.log('dddddddddddddddddd')
    console.log(util.isObject(userinfo))
    if (userinfo == null || !util.isObject(userinfo) || util.isUndefined(userinfo.token) || userinfo.token == null) {
      token = '';
    } else {
      token = util.isObject(userinfo) ? userinfo.token : '';
    }
  
    
    if (userinfo && typeof userinfo == 'object' && token) {
      // 还要检查token是否过期
      let date = new Date();
      var now = date.getTime() / 1000;
      if (now >= (userinfo.expiretime - 20)) {
        return base64.baseEncode(key + '_');
      } else {
        return token;
      }
    } else {
      return base64.baseEncode(key + '_');
    }
  },
  //微信登陆
  login: function (cb) {
    var that = this;
    //调用登录接口
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) {
          //发起网络请求
          wx.getUserInfo({
            success: function (ures) {
              console.log(ures)
              http.$ajax({
                url: 'v1.user/third',
                data: {
                  code: res.code,
                  rawData: ures.rawData,
                },
                loading: '授权中...'
              }, {
                  success(res) {
                    //把用户信息保存在本地
                    typeof cb == "function" && cb(res.data);
                  },
                  fail(res) {
                    that.showLoginModal();
                  }
                })
            },
            fail: function (res) {

              that.showLoginModal(cb);
            }
          });
        } else {
          that.showLoginModal(cb);
        }
      }
    });
  },
  //显示登录或授权提示
  showLoginModal: function (cb) {
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '当前无法获取到你的个人信息，无法正常登陆，请重新授权',
      showCancel: false,
      success: function (res) {

      }
    });
  },
  //构造CDN地址
  cdnurl: function (url) {
    return url.toString().match(/^https?:\/\/(.*)/i) ? url : this.globalData.image_url + url;
  },
  processImg(list, field = 'image') {
    let that = this;
    if (util.isArray(list)) {
      console.log(list);
      list.forEach(function (item, i) {
        console.log(item);
        if (util.isObject(item)) {
          //值为对象
          if (!util.isUndefined(item[field])) {
            item[field] = that.cdnurl(item[field]);
          }
        } else {
          //值为字符串
          if (util.isString(item)) {
            list[i] = that.cdnurl(item)
          }
        }
      })

    } else if (util.isObject(list)) {
      if (!util.isUndefined(list[field])) {
        list[field] = that.cdnurl(list[field]);
      }
    } else {
      //字符串  
      if (util.isString(item)) {
        list = that.cdnurl(list);
      }
    }

    return list;
  },
  //文本提示
  info: function (msg, cb) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      complete: function () {
        typeof cb == "function" && cb();
      }
    });
  },
  //成功提示
  success: function (msg, cb) {
    wx.showToast({
      title: msg,
      icon: 'success',
      image: '/images/ok.png',
      duration: 2000,
      complete: function () {
        typeof cb == "function" && cb();
      }
    });
  },
  //错误提示
  error: function (msg, cb) {
    wx.showToast({
      title: msg,
      image: '/images/error.png',
      duration: 2000,
      complete: function () {
        typeof cb == "function" && cb();
      }
    });
  },
  //警告提示
  warning: function (msg, cb) {
    wx.showToast({
      title: msg,
      image: '/images/warning.png',
      duration: 2000,
      complete: function () {
        typeof cb == "function" && cb();
      }
    });
  },
  onShow: function (e) {

  },

})